﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class PageBrowser : UserControl
    {
        

        #region Constructors

        public PageBrowser()
        {
            InitializeComponent();            

            InitThumbnail(Root, "May07");
            InitThumbnail(Root, "Jun07");
            InitThumbnail(Root, "Jul07");
            InitThumbnail(Root, "Aug07");
            InitThumbnail(Root, "Sept07");
            InitThumbnail(Root, "Oct07");
            InitThumbnail(Root, "Nov07");

            

            Root.MouseEnter += new MouseEventHandler(OnMouseEnter);
            Root.MouseLeave += new MouseEventHandler(OnMouseLeave);

            BrowserFlyback.Completed += new EventHandler(OnFlybackCompleted);

            BrowserFlyout.Completed += new EventHandler(OnFlyoutCompleted);

        }

        void OnFlybackCompleted(object sender, EventArgs e)
        {
            flyingBack = false;
        }

        void OnFlyoutCompleted(object sender, EventArgs e)
        {
            flyingOut = false;
        }

        bool flyingBack = false;
        bool flyingOut = false;
        bool mouseOver = false;

        void OnMouseLeave(object sender, EventArgs e)
        {
            Storyboard flyBack = (Storyboard)Root.FindName("BrowserFlyback");
            Storyboard flyOut = (Storyboard)Root.FindName("BrowserFlyout");

            mouseOver = false;

            if (!flyingOut && !flyingBack)
            {
                flyingBack = true;
                flyBack.Begin();
            }
        }

        void OnMouseEnter(object sender, MouseEventArgs e)
        {
            Storyboard flyBack = (Storyboard)Root.FindName("BrowserFlyback");
            Storyboard flyOut = (Storyboard)Root.FindName("BrowserFlyout");

            mouseOver = true;

            if (!flyingOut && !flyingBack)
            {
                flyingOut = true;
                flyOut.Begin();
            }

        }

        Canvas currOver;


        #endregion Constructors

        #region Public Methods

        private void OnThumbnailMouseLeave(object sender, EventArgs e)
        {
            currOver = (Canvas)sender;

            Storyboard sb = (Storyboard)Root.FindName(String.Format("ScaleDown{0}", currOver.Name));
            TextBlock tb = (TextBlock)Root.FindName(String.Format("{0}Text", currOver.Name));
            SolidColorBrush b = new SolidColorBrush(Color.FromArgb(0xFF, 0x1D, 0x62, 0xAF));
            tb.Foreground = b;

            try
            {
                sb.Begin();
            }
            catch  
            {
            }
        }

        private void OnThumbnailMouseEnter(object sender, MouseEventArgs e)
        {
            currOver = (Canvas)sender;
            Storyboard sb = (Storyboard)Root.FindName(String.Format("ScaleUp{0}", currOver.Name));
            TextBlock tb = (TextBlock)Root.FindName(String.Format("{0}Text", currOver.Name));
            SolidColorBrush b = new SolidColorBrush(Color.FromArgb(byte.MaxValue, 0, 0, 0));
            tb.Foreground = b;
            sb.Begin();
        }

        private void InitThumbnail(FrameworkElement control, string name)
        {
            Canvas thumbnail = (Canvas)control.FindName(name);
            thumbnail.MouseEnter += new MouseEventHandler(OnThumbnailMouseEnter);
            thumbnail.MouseLeave += new MouseEventHandler(OnThumbnailMouseLeave);
            thumbnail.MouseLeftButtonDown += new MouseButtonEventHandler(OnThumbnailMouseLeftButtonDown);

            Canvas c = (Canvas)control;

            string storyboard = String.Format(@"<Storyboard xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='ScaleUp{0}'>", name);
            storyboard += String.Format(@"<DoubleAnimation Duration='0:0:0.1' From='1' To='1.3' Storyboard.TargetName='{0}Scale' Storyboard.TargetProperty='(ScaleX)'/>", name);
            storyboard += String.Format(@"<DoubleAnimation Duration='0:0:0.1' From='1' To='1.3' Storyboard.TargetName='{0}Scale' Storyboard.TargetProperty='(ScaleY)'/>", name);
            storyboard += String.Format(@"<DoubleAnimation Duration='0:0:0.1' From='0' To='-60' Storyboard.TargetName='{0}Translate' Storyboard.TargetProperty='(X)'/>", name);
            storyboard += String.Format(@"<DoubleAnimation Duration='0:0:0.1' From='0' To='-90' Storyboard.TargetName='{0}Translate' Storyboard.TargetProperty='(Y)'/>", name);
            storyboard += String.Format(@"</Storyboard>");


            Storyboard storyboardDependency = XamlLoader<Storyboard>.Create(storyboard); 
            c.Resources.Add((string)storyboardDependency.GetValue(Canvas.NameProperty), storyboardDependency);

            storyboard = String.Format(@"<Storyboard xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='ScaleDown{0}'>", name);
            storyboard += String.Format(@"<DoubleAnimation Duration='0:0:0.1' From='1.3' To='1' Storyboard.TargetName='{0}Scale' Storyboard.TargetProperty='(ScaleX)'/>", name);
            storyboard += String.Format(@"<DoubleAnimation Duration='0:0:0.1' From='1.3' To='1' Storyboard.TargetName='{0}Scale' Storyboard.TargetProperty='(ScaleY)'/>", name);
            storyboard += String.Format(@"<DoubleAnimation Duration='0:0:0.1' From='-60' To='0' Storyboard.TargetName='{0}Translate' Storyboard.TargetProperty='(X)'/>", name);
            storyboard += String.Format(@"<DoubleAnimation Duration='0:0:0.1' From='-90' To='0' Storyboard.TargetName='{0}Translate' Storyboard.TargetProperty='(Y)'/>", name);
            storyboard += String.Format(@"</Storyboard>");

            storyboardDependency = XamlLoader<Storyboard>.Create(storyboard) ;
            c.Resources.Add((string)storyboardDependency.GetValue(Canvas.NameProperty), storyboardDependency);

        }

        void OnThumbnailMouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            ThumbnailMouseLeftButtonDown(sender, e);
        }

        private void OnThumbnailClicked(object sender)
        {
        }

        #endregion Public Methods

        #region Public Properties

        private bool isPageBrowserScrolling;

        public bool IsPageBrowserScrolling
        {
            get
            {
                return isPageBrowserScrolling;
            }
            set
            {
                isPageBrowserScrolling = value;
            }
        }

        private int maxNumPages;

        public int MaxNumPages
        {
            get
            {
                return maxNumPages;
            }
            set
            {
                maxNumPages = value;
            }
        }

        #endregion Public Properties

        #region Public Events

        public event EventHandler ThumbnailMouseLeftButtonDown;

        #endregion Public Events
    }
}
